package com.icinfo.cloud.provider.caseflow.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONObject;
import com.icinfo.cloud.provider.common.constant.Constant;
import com.icinfo.cloud.provider.common.dicenum.CaseStagePublicServiceEnum;
import com.icinfo.cloud.provider.common.enums.TerritoryCodeEnum;
import com.icinfo.cloud.provider.common.exception.BusinessException;
import com.icinfo.cloud.provider.handler.ApplicationContextRegister;
import com.icinfo.cloud.provider.punish.approval.service.ICaseApprovalWaitRecordService;
import com.icinfo.cloud.provider.punish.config.dto.CaseRecallOperateDto;
import com.icinfo.cloud.provider.punish.config.dto.ConfigEndDayQueryDto;
import com.icinfo.cloud.provider.punish.config.service.IConfigBusiCaseRecallColumnService;
import com.icinfo.cloud.provider.punish.config.service.IConfigWorkRestDayInfoService;
import com.icinfo.cloud.provider.punish.ucase.casemain.dto.CaseMainDetailAuxiliaryInfoRequest;
import com.icinfo.cloud.provider.punish.ucase.casemain.enums.CaseStatusEnum;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseMainDetailAuxiliaryInfo;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseMainDetailInfo;
import com.icinfo.cloud.provider.punish.ucase.casemain.model.CaseMainInfo;
import com.icinfo.cloud.provider.punish.ucase.casemain.service.*;
import com.icinfo.cloud.provider.punish.ucase.casemain.vo.CaseMainInfoVo;
import com.icinfo.cloud.provider.punish.ucaseassist.assistmain.service.ICaseAssistMainInfoService;
import com.icinfo.cloud.workflow.config.model.ConfigWorkFlowBranch;
import com.icinfo.cloud.workflow.config.model.ConfigWorkFlowDetailInfo;
import com.icinfo.cloud.workflow.config.model.ConfigWorkFlowOperateDetailInfo;
import com.icinfo.cloud.workflow.config.service.IConfigWorkFlowBranchService;
import com.icinfo.cloud.workflow.config.service.IConfigWorkFlowDocumentSentService;
import com.icinfo.cloud.workflow.config.service.IConfigWorkFlowDocumentService;
import com.icinfo.cloud.workflow.config.service.IConfigWorkFlowOperateDetailInfoService;
import com.icinfo.cloud.workflow.work.model.WorkFlowRollAssistMainInfo;
import com.icinfo.cloud.workflow.work.model.WorkFlowRollAutoexcuteRecord;
import com.icinfo.cloud.workflow.work.model.WorkFlowRollMainInfo;
import com.icinfo.cloud.workflow.work.model.WorkFlowRollRecordInfo;
import com.icinfo.cloud.workflow.work.service.IWorkFlowRollBusiDetailService;
import com.icinfo.framework.tools.utils.MapUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.time.LocalDate;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述: 工作流当前环节表 work_flow_roll_main_info 对应的Service接口实现类.<br>
 *
 * @author tianrunjia
 * @date 2022年05月19日
 */
@Primary
@Service
@Slf4j
public class CaseFlowRollBusiDetailServiceImpl implements IWorkFlowRollBusiDetailService {

    @Autowired
    private ICaseMainInfoCommonService caseMainInfoCommonService;
    @Autowired
    private IConfigWorkRestDayInfoService configWorkRestDayInfoService;
    @Autowired
    private IConfigWorkFlowDocumentService configWorkFlowDocumentService;
    @Autowired
    private ApplicationContextRegister applicationContextRegister;
    @Autowired
    private IConfigWorkFlowDocumentSentService configWorkFlowDocumentSentService;
    @Autowired
    private ICaseMainInfoService caseMainInfoService;
    @Autowired
    private ICaseAppendixRecordService caseAppendixRecordService;
    @Autowired
    private ICaseDocumentRecordService caseDocumentRecordService;
    @Autowired
    private ICaseApprovalWaitRecordService caseApprovalWaitRecordService;
    @Resource
    private ICaseTemporaryRecordService caseTemporaryRecordService;
    @Autowired
    private ICaseAssistMainInfoService caseAssistMainInfoService;
    @Autowired
    private IConfigWorkFlowOperateDetailInfoService configWorkFlowOperateDetailInfoService;
    @Autowired
    private IConfigWorkFlowBranchService configWorkFlowBranchService;
    @Resource
    private IConfigBusiCaseRecallColumnService configBusiCaseRecallColumnService;
    @Resource
    private ICaseMainDetailAuxiliaryInfoService caseMainDetailAuxiliaryInfoService;
    @Resource
    private ICaseOfflineDocumentRecordService caseOfflineDocumentRecordService;
    @Resource
    private ICaseMainDetailInfoService caseMainDetailInfoService;
    @Resource
    private ICaseReasonMainRecordService caseReasonMainRecordService;
    /**
     * 【V1.0.0】描述：获取案件是否进行流程滚动
     * @author tianrunjia
     * @date 2022-05-19
     * @return
     */
    @Override
    public boolean getIsRollWorkFlow(String caseId, Map annoMap, ConfigWorkFlowDetailInfo configWorkFlowDetailInfo) {
        String catalogCode = annoMap.get("documentCatalogCode")==null?"":annoMap.get("documentCatalogCode").toString();
        log.info("当前滚动的案件文书：【" + catalogCode + "】");
        String closeSignal = annoMap.get("closeSignal")==null?"":annoMap.get("closeSignal").toString();
        log.info("当前滚动的案件流信号：【" + closeSignal + "】");

        boolean isRoll = false;
        if("1".equals(configWorkFlowDetailInfo.getWorkLinkType())){ //处置环节
            if(StringUtils.isNotEmpty(closeSignal) && closeSignal.equals(configWorkFlowDetailInfo.getCloseSignal()) && StringUtils.isNotEmpty(catalogCode)){
                //查询当前文书是否为必填文书，如果是必填文书，才会触发后续信号的判断
                String isMustDocument = configWorkFlowDocumentService.getCurrentDocumentIsLinkHandleMust(configWorkFlowDetailInfo.getWorkFlowCode(),
                        configWorkFlowDetailInfo.getWorkLinkCode(),configWorkFlowDetailInfo.getWorkFlowVersion(),catalogCode);
                if("1".equals(isMustDocument)){
                    isRoll = true;
                }
            }
        }else if("2".equals(configWorkFlowDetailInfo.getWorkLinkType())){ //审批环节
            if(StringUtils.isNotEmpty(closeSignal) && closeSignal.equals(configWorkFlowDetailInfo.getCloseSignal())  && StringUtils.isNotEmpty(catalogCode)){
                //查询当前文书是否为必填文书，如果是必填文书，才会触发后续信号的判断
                String isMustDocument = configWorkFlowDocumentService.getCurrentDocumentIsLinkHandleMust(configWorkFlowDetailInfo.getWorkFlowCode(),
                        configWorkFlowDetailInfo.getWorkLinkCode(),configWorkFlowDetailInfo.getWorkFlowVersion(),catalogCode);
                if("1".equals(isMustDocument)){
                    isRoll = true;
                    //审核通过后特殊处理
                    ICaseStageOperatePublicService caseStageOperatePublicService = null;
                    if(!ObjectUtils.isEmpty(CaseStagePublicServiceEnum.getServiceNameByCode(configWorkFlowDetailInfo.getWorkLinkCode()))){
                        caseStageOperatePublicService = (ICaseStageOperatePublicService)applicationContextRegister
                                .getBeanById(CaseStagePublicServiceEnum.getServiceNameByCode(configWorkFlowDetailInfo.getWorkLinkCode()));
                        caseStageOperatePublicService.doCaseStageOperate(caseId,catalogCode);
                    }
                }
            }
        }else if("3".equals(configWorkFlowDetailInfo.getWorkLinkType())){ //送达环节
            //查询当前送达的文书是否在送达文书的列表中，是则滚动
            /*
                送达环节滚动前提条件
                1、文书目录编码不能为空
                2、传入的信号（closeSignal）必须为CLOSE-TRIGGER-SENT、CLOSE-TRIGGER-SIGNSTAMPED
                3、送达文书需在文书送达环节配置表中有配置
             */
            if (StringUtils.isNotEmpty(catalogCode) && ("CLOSE-TRIGGER-SENT".equals(closeSignal) || "CLOSE-TRIGGER-SIGNSTAMPED".equals(closeSignal))) {
                int sentDocumentCount = configWorkFlowDocumentSentService.getIsDocumentInLinkSentList(configWorkFlowDetailInfo.getWorkFlowCode(),
                        configWorkFlowDetailInfo.getWorkLinkCode(), configWorkFlowDetailInfo.getWorkFlowVersion(), catalogCode);
                if (sentDocumentCount > 0) {
                    isRoll = true;
                }
            }
        }
        return isRoll;
    }

    /**
     * 【V1.0.0】描述：获取案件分支流程下一环节编码
     * @author tianrunjia
     * @date 2022-05-19
     * @return
     */
    @Override
    public String getWorkFlowNextLinkCodeWithBranch(String caseId, String partyId, ConfigWorkFlowBranch configWorkFlowBranch) {
        String nextLinkCode = "";
        if(ObjectUtil.isNotEmpty(configWorkFlowBranch)){
            String conditionContent = configWorkFlowBranch.getJudgeConditionContent();
            //String judgeConditionTypeCode=configWorkFlowBranch.getJudgeConditionTypeCode();
            int judgeNum=Integer.parseInt(configWorkFlowBranch.getJudgeNum());
            /*if("IS_COLLECTIVEDISCUSS".equals(judgeConditionTypeCode)){*/
            for (int i=0;i<judgeNum;i++){
                Map<String,String> resultCodeMap=new HashMap<>();
                resultCodeMap=getNextLinkCodeByConditionContent(conditionContent,caseId,partyId);
                nextLinkCode=MapUtils.getString(resultCodeMap,"nextLinkCode","");
                if(StringUtils.isNotBlank(nextLinkCode)){
                    break;
                }else {
                    conditionContent=MapUtils.getString(resultCodeMap,"conditionContent","");
                }
            }
                /*Map<String,String> resultCodeMap=new HashMap<>();
                //map.put("conditionContent",conditionContent);
                //map.put("nextLinkCode",nextLinkCode);
                resultCodeMap=getNextLinkCodeByConditionContent(conditionContent,caseId,partyId);
                nextLinkCode=MapUtils.getString(resultCodeMap,"nextLinkCode","");
                if(StringUtils.isBlank(nextLinkCode)){
                    conditionContent=MapUtils.getString(resultCodeMap,"conditionContent","");
                    resultCodeMap=getNextLinkCodeByConditionContent(conditionContent,caseId,partyId);
                    nextLinkCode=MapUtils.getString(resultCodeMap,"nextLinkCode","");
                    if(StringUtils.isBlank(nextLinkCode)){
                        conditionContent=MapUtils.getString(resultCodeMap,"conditionContent","");
                        resultCodeMap=getNextLinkCodeByConditionContent(conditionContent,caseId,partyId);
                        nextLinkCode=MapUtils.getString(resultCodeMap,"nextLinkCode","");
                    }
                }*/
                /*Map<String, String> condMap = (Map<String, String>) JSONObject.parse(conditionContent);
                String judgeColumn = condMap.get("JUDGECOLUMN");
                String judgeTable = condMap.get("JUDGETABLE");
                String judgeWhere = condMap.get("JUDGEWHERE");
                if("invest_conclusion".equals(judgeColumn)){ // 处罚决定会分别处罚，存在多当事人多情况
                    CaseMainInfoVo caseMainInfo = caseMainInfoService.getCaseMainInfo(caseId);
                    if("3".equals(caseMainInfo.getCasePartiesReasonType()) && "2".equals(caseMainInfo.getHandleType())) { //如果是多当事人
                        judgeWhere = judgeWhere + "party_id = '" + partyId + "'";
                        judgeTable = condMap.get("JUDGETABLEOTHER");
                    }
                }
                Map<String,Object> contentMap = caseMainInfoCommonService.getCaseMainInfoSomeDataFromSomeTable(judgeColumn, caseId,
                        judgeTable,judgeWhere);

                String colldisResult = contentMap.get(judgeColumn)!=null?contentMap.get(judgeColumn).toString():"";
                if(Constant.STRING_1.equals(colldisResult)){
                    nextLinkCode = condMap.get(colldisResult).toString();
                }else{
                    condMap.get(colldisResult).toString();
                }*/
           /* }
            if("IS_INVEST_CONCLUSION".equals(judgeConditionTypeCode)){
                Map<String,String> resultCodeMap=new HashMap<>();
                resultCodeMap=getNextLinkCodeByConditionContent(conditionContent,caseId,partyId);
                nextLinkCode=MapUtils.getString(resultCodeMap,"nextLinkCode","");
            }*/
            /*String conditionContent = configWorkFlowBranch.getJudgeConditionContent();
            String resultContent = configWorkFlowBranch.getJudgeResultContent();
            Map<String, String> condMap = (Map<String, String>) JSONObject.parse(conditionContent);
            Map<String, String> resultMap = (Map<String, String>) JSONObject.parse(resultContent);

            String judgeColumn = condMap.get("JUDGECOLUMN");
            String judgeTable = condMap.get("JUDGETABLE");
            String judgeWhere = condMap.get("JUDGEWHERE");
            if("IS_INVEST_CONCLUSION".equals(configWorkFlowBranch.getJudgeConditionTypeCode())){ // 处罚决定会分别处罚，存在多当事人多情况
                CaseMainInfoVo caseMainInfo = caseMainInfoService.getCaseMainInfo(caseId);
                if("3".equals(caseMainInfo.getCasePartiesReasonType()) && "2".equals(caseMainInfo.getHandleType())) { //如果是多当事人
                    judgeWhere = judgeWhere + "party_id = '" + partyId + "'";
                    judgeTable = condMap.get("JUDGETABLEOTHER");
                }
            }

            Map<String,Object> contentMap = caseMainInfoCommonService.getCaseMainInfoSomeDataFromSomeTable(judgeColumn, caseId,
                    judgeTable,judgeWhere);

            String colldisResult = contentMap.get(judgeColumn)!=null?contentMap.get(judgeColumn).toString():"";
            if(StringUtils.isNotBlank(colldisResult)){
                nextLinkCode = resultMap.get(colldisResult).toString();
            }*/
        }else {
            throw new BusinessException("分支流程配置缺失！");
        }
        return nextLinkCode;
    }

    private Map<String,String> getNextLinkCodeByConditionContent(String conditionContent,String caseId,String partyId) {
        Map<String,String> map=new HashMap<>();
        String nextLinkCode="";

        Map<String, String> condMap = (Map<String, String>) JSONObject.parse(conditionContent);
        String judgeColumn = condMap.get("JUDGECOLUMN");
        String judgeTable = condMap.get("JUDGETABLE");
        String judgeWhere = condMap.get("JUDGEWHERE");
        boolean isPunish=false;
        if("invest_conclusion".equals(judgeColumn)){ // 处罚决定会分别处罚，存在多当事人多情况
            CaseMainInfoVo caseMainInfo = caseMainInfoService.getCaseMainInfo(caseId);
            if("3".equals(caseMainInfo.getCasePartiesReasonType()) && "2".equals(caseMainInfo.getHandleType())) { //如果是多当事人
                CaseMainDetailAuxiliaryInfoRequest request=new CaseMainDetailAuxiliaryInfoRequest();
                request.setCaseId(caseId);
                List<CaseMainDetailAuxiliaryInfo> caseMainDetailAuxiliaryInfos=caseMainDetailAuxiliaryInfoService.getCaseMainDetailAuxiliaryInfo(request);
                for (CaseMainDetailAuxiliaryInfo caseMainDetailAuxiliaryInfo:caseMainDetailAuxiliaryInfos) {
                    if("1".equals(caseMainDetailAuxiliaryInfo.getInvestConclusion())){
                        isPunish=true;
                        break;
                    }
                }
                if(StringUtils.isNotBlank(partyId)) {
                    judgeWhere = judgeWhere + "party_id = '" + partyId + "'";
                    judgeTable = condMap.get("JUDGETABLEOTHER");
                }else{
                    if(isPunish){
                        nextLinkCode=condMap.get("1");
                    }else {
                        nextLinkCode=condMap.get("2");
                    }
                    map.put("conditionContent","");
                    map.put("nextLinkCode",nextLinkCode);
                    return map;
                    /*if(Constant.TZ_BUSI_PARAM_ID.equals(busiParamId)){
                        map.put("conditionContent","");
                        map.put("nextLinkCode","LINK-202203020944");
                        return map;
                    }else {
                        if(isPunish){
                            map.put("conditionContent","");
                            map.put("nextLinkCode","LINK-202207280944");
                        }else{
                            map.put("conditionContent","");
                            map.put("nextLinkCode","LINK-202207290944");
                        }
                        return map;
                    }*/


                }
            }
        }
        Map<String,Object> contentMap = caseMainInfoCommonService.getCaseMainInfoSomeDataFromSomeTable(judgeColumn, caseId,
                judgeTable,judgeWhere);
        String colldisResult="0";
        if(null!=contentMap) {
            colldisResult = contentMap.get(judgeColumn) != null ? contentMap.get(judgeColumn).toString() : "";
        }
        if(Constant.STRING_1.equals(colldisResult)||Constant.STRING_2.equals(colldisResult)){
            nextLinkCode = String.valueOf(condMap.get(colldisResult));
        }else{
            conditionContent=String.valueOf(condMap.get(colldisResult));
            if(conditionContent.startsWith("LINK-")){
                nextLinkCode=conditionContent;
            }
        }
        map.put("conditionContent",conditionContent);
        map.put("nextLinkCode",nextLinkCode);
        return map;
    }
    /**
     * 【V1.0.0】描述：获取案件分支流程下一环节编码
     * @author tianrunjia
     * @date 2022-05-19
     * @return
     */

    /*public String getWorkFlowNextLinkCodeWithBranch(String caseId, String partyId, List<ConfigWorkFlowBranch> configWorkFlowBranchList) {
        String nextLinkCode = "";
        for (ConfigWorkFlowBranch configWorkFlowBranch:configWorkFlowBranchList) {
            if(ObjectUtil.isNotEmpty(configWorkFlowBranch)){
                String conditionContent = configWorkFlowBranch.getJudgeConditionContent();
                String resultContent = configWorkFlowBranch.getJudgeResultContent();
                Map<String, String> condMap = (Map<String, String>) JSONObject.parse(conditionContent);
                Map<String, String> resultMap = (Map<String, String>) JSONObject.parse(resultContent);

                String judgeColumn = condMap.get("JUDGECOLUMN");
                String judgeTable = condMap.get("JUDGETABLE");
                String judgeWhere = condMap.get("JUDGEWHERE");
                if("IS_INVEST_CONCLUSION".equals(configWorkFlowBranch.getJudgeConditionTypeCode())){ // 处罚决定会分别处罚，存在多当事人多情况
                    CaseMainInfoVo caseMainInfo = caseMainInfoService.getCaseMainInfo(caseId);
                    if("3".equals(caseMainInfo.getCasePartiesReasonType()) && "2".equals(caseMainInfo.getHandleType())) { //如果是多当事人
                        judgeWhere = judgeWhere + "party_id = '" + partyId + "'";
                        judgeTable = condMap.get("JUDGETABLEOTHER");
                    }
                }

                Map<String,Object> contentMap = caseMainInfoCommonService.getCaseMainInfoSomeDataFromSomeTable(judgeColumn, caseId,
                        judgeTable,judgeWhere);

                String colldisResult = contentMap.get(judgeColumn)!=null?contentMap.get(judgeColumn).toString():"";
                if(StringUtils.isNotBlank(colldisResult)){
                    nextLinkCode = resultMap.get(colldisResult).toString();
                    break;
                }
            }else {
                throw new BusinessException("分支流程配置缺失！");
            }
        }
        return nextLinkCode;
    }*/

    /**
     * 【V1.0.0】描述：获取自动执行流程下一环节编码
     * @author tianrunjia
     * @date 2022-05-24
     * @return
     */
    @Override
    public String getWorkFlowNextLinkCodeWithAutoexcute(WorkFlowRollAutoexcuteRecord workFlowRollAutoexcuteRecord, String excuteType) {
        if(Constant.EXECUTE_PUNISHINFORM_SENT.equals(excuteType)){
            try {
                Map<String, String> condMap = (Map<String, String>) JSONObject.parse(workFlowRollAutoexcuteRecord.getWorkAutoexcuteCondition());

                Integer dayCount = null;
                String documentCatalogCode = "";
                String preConditionColumn = condMap.get("PRECONDITIONCOLUMN").toString();
                String investconColumn = condMap.get("INVESTCONCOLUMN").toString();
                //告知书期限
                String punishBeforeInformDayColumn = condMap.get("PUNISHBEFOREINFORMDAYCOLUMN");
                //听证类告知书期限
                String punishInformDayColumn = condMap.get("PUNISHINFORMDAYCOLUMN");
                String queryColumn = preConditionColumn + "," + investconColumn;
                if (!ObjectUtils.isEmpty(punishBeforeInformDayColumn) && !ObjectUtils.isEmpty(punishInformDayColumn)) {
                    queryColumn += "," + punishBeforeInformDayColumn + "," + punishInformDayColumn;
                }
                String whereCondition = "";
                //PRECONDITIONCOLUMN  - 是否听证  PRECONDITIONCOLUMN-TRUE / PRECONDITIONCOLUMN-FALSE
                //JUDGECOLUMN - 判断依据字段 JUDGETABLE - 判断依据表
                //PRECONDITIONCOLUMN-TRUE-CATALOGCODE / - PRECONDITIONCOLUMN-FALSE-CATALOGCODE 目录编码

                CaseMainInfoVo caseMainInfo = caseMainInfoService.getCaseMainInfo(workFlowRollAutoexcuteRecord.getWorkId());
                Map<String,Object> caseMainDetailMap = new HashMap<>();
                if("3".equals(caseMainInfo.getCasePartiesReasonType()) && "2".equals(caseMainInfo.getHandleType())){ //如果是多当事人 并且分别处罚
                    caseMainDetailMap = caseMainInfoCommonService.getCaseMainInfoSomeDataFromSomeTable(queryColumn,
                            workFlowRollAutoexcuteRecord.getWorkId(),"upcase.case_main_detail_auxiliary_info","party_id='" + workFlowRollAutoexcuteRecord.getWorkSubId()+"'");
                }else {
                    caseMainDetailMap = caseMainInfoCommonService.getCaseMainInfoSomeDataFromSomeTable(queryColumn,
                            workFlowRollAutoexcuteRecord.getWorkId(),"upcase.case_main_detail_info","");
                }


                //是否需要听证
                if("0".equals(caseMainDetailMap.get(preConditionColumn).toString())){
                    //改造为获取生成告知书选择的期限
                    if (!ObjectUtils.isEmpty(caseMainDetailMap.get(punishBeforeInformDayColumn))) {
                        dayCount = Integer.parseInt(caseMainDetailMap.get(punishBeforeInformDayColumn).toString());
                    } else {
                        dayCount = Integer.parseInt(condMap.get("PRECONDITIONCOLUMN-FALSE").toString());
                    }
                    documentCatalogCode = condMap.get("PRECONDITIONCOLUMN-FALSE-CATALOGCODE").toString();
                }else if("1".equals(caseMainDetailMap.get(preConditionColumn).toString())){
                    //改造为获取生成告知书(听证类)选择的期限
                    if (!ObjectUtils.isEmpty(caseMainDetailMap.get(punishInformDayColumn))) {
                        dayCount = Integer.parseInt(caseMainDetailMap.get(punishInformDayColumn).toString());
                    } else {
                        dayCount = Integer.parseInt(condMap.get("PRECONDITIONCOLUMN-TRUE").toString());
                    }
                    documentCatalogCode = condMap.get("PRECONDITIONCOLUMN-TRUE-CATALOGCODE").toString();
                }

                if("3".equals(caseMainInfo.getCasePartiesReasonType()) && "2".equals(caseMainInfo.getHandleType())){
                    //获取送达日期条件
                    whereCondition = "document_record_status='1' and document_catalog_code = '" + documentCatalogCode + "' and party_id = '" + workFlowRollAutoexcuteRecord.getWorkSubId() + "'";
                }else {
                    whereCondition = "document_record_status='1' and document_catalog_code = '" + documentCatalogCode + "'";
                }

                //获取送达日期
                Map<String,Object> documentRecordMap = caseMainInfoCommonService.getCaseMainInfoSomeDataFromSomeTable(condMap.get("JUDGECOLUMN").toString(), workFlowRollAutoexcuteRecord.getWorkId(),
                        condMap.get("JUDGETABLE").toString(),whereCondition);
                String  sentDate = documentRecordMap.get("sent_time").toString();

                ConfigEndDayQueryDto queryDto = new ConfigEndDayQueryDto();
                queryDto.setStartDate(sentDate.substring(0,10));
                queryDto.setDayCount(dayCount);
                queryDto.setBeforeOrAfterSign("after");
                queryDto.setType("0");
                queryDto.setIsContainStartDay("0");
                LocalDate resultDate = configWorkRestDayInfoService.getPeriodEndDate(queryDto);

                if(LocalDate.now().isAfter(resultDate)){
                    //案件主表更新 及 环节记录保存
                    //人社行政处理自动流转优化
                    if (ObjectUtil.isNotEmpty(caseMainDetailMap.get(investconColumn))) {
                        if("2".equals(caseMainDetailMap.get(investconColumn).toString())){ //不予处罚
                            return condMap.get("NO-NEXTLINKCODE").toString();
                        }else if("1".equals(caseMainDetailMap.get(investconColumn).toString())){ //予以处罚
                            return condMap.get("YES-NEXTLINKCODE").toString();
                        }
                    } else if (caseMainInfo.getLineCode().contains(TerritoryCodeEnum.RLSB.getCode())) {
                        //人社条线行政处理无调查结论, 直接取下一环节
                        return condMap.get("YES-NEXTLINKCODE").toString();
                    }

                }
            }catch (Exception e) {
                System.out.println("数据处理异常！【" + workFlowRollAutoexcuteRecord.getWorkId() + "," + workFlowRollAutoexcuteRecord.getWorkSubId() + "】");
            }
        }
        return "";
    }

    /**
     * 【V1.0.0】描述：工作流环节开始数据更新
     * @author tianrunjia
     * @date 2022-03-02
     * @param workId
     */
    @Override
    public void doDealBusiDataWithWorkFlowRoll(String workId, WorkFlowRollMainInfo workFlowRollMainInfo) {
        CaseMainInfo caseMainInfo = new CaseMainInfo();
        caseMainInfo.setId(workId);
        caseMainInfo.setCaseStatus(workFlowRollMainInfo.getWorkBusiStatus());
        caseMainInfo.setCaseCurrentStageStarttime(workFlowRollMainInfo.getWorkCurrentStageStarttime());
        caseMainInfo.setCaseCurrentLinkStarttime(workFlowRollMainInfo.getWorkCurrentLinkStarttime());
        caseMainInfo.setCaseCurrentLinkCode(workFlowRollMainInfo.getWorkCurrentLinkCode());
        caseMainInfo.setCaseCurrentLinkName(workFlowRollMainInfo.getWorkCurrentLinkName());
        caseMainInfo.setCaseCurrentStageName(workFlowRollMainInfo.getWorkCurrentStageName());
        caseMainInfo.setCaseCurrentStageCode(workFlowRollMainInfo.getWorkCurrentStageCode());
        caseMainInfo.setCaseFlowCode(workFlowRollMainInfo.getWorkFlowCode());
        caseMainInfo.setCaseFlowVersion(workFlowRollMainInfo.getWorkFlowVersion());
        caseMainInfo.setCasePreviousLinkCode(workFlowRollMainInfo.getWorkPreviousLinkCode());
        caseMainInfoService.doModCaseMainInfoWithCaseFlowRoll(caseMainInfo);
        //结案时推送案件到外部系统
        if (CaseStatusEnum.END_CASE.getCode().equals(caseMainInfo.getCaseStatus())) {
            caseMainInfoService.pushCaseToOutSys(workId);
        }
    }

    /**
     * 【V1.1.0】描述：环节撤回时，案件主表撤回
     * @author tianrunjia
     * @date 2022-06-27
     * @return
     */
    @Override
    public void doDealBusiDataWithWorkLinkRecall(String s, WorkFlowRollRecordInfo workFlowRollRecordInfo) {
        CaseMainInfo caseMainInfo = new CaseMainInfo();
        caseMainInfo.setId(s);
        caseMainInfo.setCaseCurrentStageCode(workFlowRollRecordInfo.getWorkStageCode());
        caseMainInfo.setCaseCurrentStageName(workFlowRollRecordInfo.getWorkStageName());
        caseMainInfo.setCaseCurrentLinkCode(workFlowRollRecordInfo.getWorkLinkCode());
        caseMainInfo.setCaseCurrentLinkName(workFlowRollRecordInfo.getWorkLinkName());
        caseMainInfo.setCaseCurrentLinkStarttime(workFlowRollRecordInfo.getWorkLinkStartTime());
        caseMainInfo.setCaseCurrentStageStarttime(workFlowRollRecordInfo.getWorkStageStartTime());
        //1、案件主表更新
        caseMainInfoService.doModCaseMainInfoWithCaseFlowRoll(caseMainInfo);
    }

    /**
     * 【V1.1.0】描述：环节撤回时，相关业务数据状态更新为撤回
     * @author tianrunjia
     * @date 2022-06-24
     * @return
     */
    @Override
    public void doDealBusiDataStatusWithWorkLinkRecall(String s, String s1, String s2) {
        //2、文书记录表更新
        caseDocumentRecordService.recallCaseDocumentRecordByCaseLinkCode(s,s2);
        //3、附件表更新
        caseAppendixRecordService.recallCaseAppendixRecordByCaseLinkCode(s,s2);
        //4、待审批数据撤回
        caseApprovalWaitRecordService.doRecallApprovalWaitRecordByCaseId(s);
        //TODO 4、案件详情表更新 暂理，问题非常复杂，目前让用户自己维护
        //5、暂存表案件关联数据清空
        caseTemporaryRecordService.removeAllTemporaryWithCaseId(s);
        //7、上传线下文书撤回
        caseOfflineDocumentRecordService.recallCaseOfflineDocumentByCaseLinkCode(s,s2);
        //6.撤回配置操作处理
        CaseRecallOperateDto operateDto = new CaseRecallOperateDto();
        operateDto.setCaseId(s);
        operateDto.setCaseLinkCode(s2);
        configBusiCaseRecallColumnService.operateBusiTableByRecallLinkCode(operateDto);
    }

    /**
     * 【V1.4.1】描述：根据副流程处理业务数据
     * @author tianrunjia
     * @date 2022-08-24
     * @return
     */
    @Override
    public void doDealBusiDataWithAssistLink(String s, WorkFlowRollAssistMainInfo workFlowRollAssistMainInfo, Map annoMap) {
        String catalogCode = annoMap.get("documentCatalogCode")==null?"":annoMap.get("documentCatalogCode").toString();
        log.info("当前滚动的案件文书：【" + catalogCode + "】");
        if("START".equals(s)){
            String caseAssistId = caseAssistMainInfoService.saveCaseAssistMainInfo(workFlowRollAssistMainInfo);
            //更新文书记录表
            caseDocumentRecordService.modCaseDocumentRecordWithAssistStart(workFlowRollAssistMainInfo.getWorkId(),catalogCode,caseAssistId
                ,workFlowRollAssistMainInfo.getWorkAssistCurrentStageName(),workFlowRollAssistMainInfo.getWorkAssistCurrentStageCode()
                ,workFlowRollAssistMainInfo.getWorkAssistCurrentLinkName(),workFlowRollAssistMainInfo.getWorkAssistCurrentLinkCode());
        }else {
            caseAssistMainInfoService.modCaseAssistMainInfo(workFlowRollAssistMainInfo);
        }
    }

    /**
     * 【V1.4.1】描述：获取是否进行流程滚动-NEW
     * @author tianrunjia
     * @date 2022-08-29
     * @return
     */
    @Override
    public boolean getIsWorkFlowRolling(String caseId, Map annoMap, ConfigWorkFlowOperateDetailInfo configWorkFlowOperateDetailInfo) {
        String catalogCode = annoMap.get("documentCatalogCode")==null?"":annoMap.get("documentCatalogCode").toString();
        log.info("当前滚动的案件文书：【" + catalogCode + "】");
        String closeSignal = annoMap.get("closeSignal")==null?"":annoMap.get("closeSignal").toString();
        log.info("当前滚动的案件流信号：【" + closeSignal + "】");

        if(!ObjectUtils.isEmpty(configWorkFlowOperateDetailInfo) && closeSignal.equals(configWorkFlowOperateDetailInfo.getRollSignal())){
            if("CLOSE-TRIGGER-APPROVAL-PASS".equals(closeSignal)){//审核通过后特殊处理
                ICaseStageOperatePublicService caseStageOperatePublicService = null;
                //TODO 这里的编码存在写死的情况
                if(!ObjectUtils.isEmpty(CaseStagePublicServiceEnum.getServiceNameByCode(configWorkFlowOperateDetailInfo.getWorkLinkCode()))){
                    caseStageOperatePublicService = (ICaseStageOperatePublicService)applicationContextRegister
                            .getBeanById(CaseStagePublicServiceEnum.getServiceNameByCode(configWorkFlowOperateDetailInfo.getWorkLinkCode()));
                    caseStageOperatePublicService.doCaseStageOperate(caseId,catalogCode);
                }
            }
            return true;
        }else {
            return false;
        }
    }

    /**
     * 【AJ-V1.1.1】描述：工作流环节变更数据更新
     * @param workId
     * @param workFlowRollMainInfo
     * @param caseType
     */
    @Override
    public void doDealBusiDataWithWorkFlowChange(String workId, WorkFlowRollMainInfo workFlowRollMainInfo, String caseType) {
        //更新案件工作流信息
        CaseMainInfo caseMainInfo = new CaseMainInfo();
        caseMainInfo.setId(workId);
        caseMainInfo.setCaseType(caseType);
        caseMainInfo.setCaseStatus(workFlowRollMainInfo.getWorkBusiStatus());
        caseMainInfo.setCaseCurrentLinkCode(workFlowRollMainInfo.getWorkCurrentLinkCode());
        caseMainInfo.setCaseCurrentLinkName(workFlowRollMainInfo.getWorkCurrentLinkName());
        caseMainInfo.setCaseFlowCode(workFlowRollMainInfo.getWorkFlowCode());
        caseMainInfo.setCaseFlowVersion(workFlowRollMainInfo.getWorkFlowVersion());
        caseMainInfo.setCasePreviousLinkCode(workFlowRollMainInfo.getWorkPreviousLinkCode());
        caseMainInfoService.doModCaseMainInfoWithCaseFlowRoll(caseMainInfo);
        //初始化案件详情表信息
        caseMainDetailInfoService.dealCaseMainDetailInfoOfChangeWorkFlow(workId);
        //清空处罚事项的违则罚则
        caseReasonMainRecordService.dealCaseReasonRecordOfChangeWorkFlow(workId);
        //更新文书工作流信息
        caseDocumentRecordService.dealCaseDocumentRecordOfChangeWorkFlow(workId);

    }

}
